package api

import (
	"backend/app/web/service"
	"backend/utils/jwt"
	"errors"
)

func UserCheckToken(token string) (int64, bool, bool, error) {
	//解析token
	mc, err := jwt.ParseToken(token)
	if err != nil {
		return 0, false, false, err
	}
	if !mc.IsAdmin {
		_, dbErr := service.UserService.GetUserByID(mc.UserID)
		if dbErr != nil {
			return 0, false, false, err
		}
	}
	data, err := service.UserService.GetUserTokenData(mc.UserID)
	// if mc.IsAdmin {
	// 	data, err = service.AdminService.GetAdminTokenData(mc.UserID)
	// }

	if err != nil {
		return 0, false, false, err
	}
	if data.Token != token || data.UserID != mc.UserID {
		return 0, false, false, err
	}
	if data.Forbidden == true {
		return 0, false, false, errors.New("账号被禁用")
	}
	if mc.IsAdmin != data.IsAdmin {
		return 0, false, false, errors.New("权限验证不通过")
	}
	return data.UserID, true, mc.IsAdmin, nil
}
